package Exer_HiHaiHi;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/3/23 17:02
 * 说明：省份二级联动，选省份，显示城市供选择
 */
public class CityMapTest{
    public static void main(String[] args) {
        CityMapTest cmt = new CityMapTest();
        Map map = CityMap.model;

        Set provinces = map.keySet();

        for (Object x: provinces) {
            System.out.print(x + "\t");
        }
        System.out.println();

        String[] cityArr = null;
        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.println("-----------------------------");
            System.out.println("选择省份：(输入exit以退出)");
            String prov = sc.next();
            if (prov.contains("exit")){
                break;
            }
            cityArr = (String[]) map.get(prov);
            if (cityArr == null || cityArr.length == 0){
                System.out.println("input again");
            } else {
                break;
            }
        }
        for (String s: cityArr) {
            System.out.print(s + "\t");
        }
        System.out.println();
        while(true){
            System.out.println("---选择城市：---");
            String city = sc.next();
            if (cmt.isContain(cityArr, city)){
                System.out.println("信息登记完成，您选择了: " + city);
                break;
            }
            System.out.println("City input again.");
        }

    }

    public boolean isContain(String[] cityArr, String city){
        // 方式一：增强for循环
//        for (String x: cityArr) {
//            if (x.compareTo(city) == 0) return true;
//        }
        // 方式二：普通for循环
        for (int i = 0; i < cityArr.length; i++) {
            if (city.equals(cityArr[i])) return true;
        }
        return false;
    }
}
class CityMap {
    public static Map model = new HashMap();

    static {
        model.put("北京", new String[]{"北京"});
        model.put("辽宁", new String[]{"A", "B", "C", "D", "E", "F", "G"});
        model.put("吉林", new String[]{"H", "I", "J", "K", "L", "M", "N"});
        model.put("河北", new String[]{"O", "P", "Q", "R", "S", "T", "U"});
        model.put("河南", new String[]{"V", "W", "X", "Y", "Z", "AA", "BB"});
        model.put("山东", new String[]{"CC", "DD", "EE", "FF", "GG", "HH", "II"});
    }
}
